// utils
const MongodbConnector = require('../utils/MongodbConnector')
const { initIndexes } = require('../utils/indexMangement')
const config = require('../../config')
const Model = require('../prototype/Model')
const UserLogModel = require('./UserLog')
const mongodbConfig = config.mongodb
const mongodbConnector = new MongodbConnector()

const models = {}
module.exports = {
  async allModelInit () {
    // 初始化数据库连接
    await mongodbConnector.init(mongodbConfig)

    // 51 提效
    models.User = new Model({ mongodbConnector, collectionName: 'User' })
    models.UserLog = new UserLogModel({ mongodbConnector, collectionName: 'UserLog' })
    models.UserStorage = new Model({ mongodbConnector, collectionName: 'UserStorage' })
    models.UserContent = new Model({ mongodbConnector, collectionName: 'UserContent' })
    models.KmWoshipm = new Model({ mongodbConnector, collectionName: 'KmWoshipm' })
    models.KmNewrank = new Model({ mongodbConnector, collectionName: 'KmNewrank' })
    models.KmSpace = new Model({ mongodbConnector, collectionName: 'KmSpace' })
    models.Redirect = new Model({ mongodbConnector, collectionName: 'Redirect' })
    models.MsgChannel = new Model({ mongodbConnector, collectionName: 'MsgChannel' })
    models.UserTophub = new Model({ mongodbConnector, collectionName: 'UserTophub' })
    models.Tophub = new Model({ mongodbConnector, collectionName: 'Tophub', dbName: 'dw' })
    models.TophubRankChange = new Model({ mongodbConnector, collectionName: 'tophub_rpt_rank_change', dbName: 'dw' })
    models.Ads = new Model({ mongodbConnector, collectionName: 'Ads' })
    models.UserFeedback = new Model({ mongodbConnector, collectionName: 'UserFeedback' })
    models.MsgRobot = new Model({ mongodbConnector, collectionName: 'MsgRobot' })
    models.Tool = new Model({ mongodbConnector, collectionName: 'Tool' })
    models.ToolTag = new Model({ mongodbConnector, collectionName: 'ToolTag' })
    models.Topic = new Model({ mongodbConnector, collectionName: 'Topic' })
    models.ExternalOrigin = new Model({ mongodbConnector, collectionName: 'ExternalOrigin' })
    models.EditLog = new Model({ mongodbConnector, collectionName: 'EditLog' })
    models.WorkbenchApps = new Model({ mongodbConnector, collectionName: 'WorkbenchApps' })
    models.WorkbenchTask = new Model({ mongodbConnector, collectionName: 'WorkbenchTask' })
    models.City = new Model({ mongodbConnector, collectionName: 'City' })
    models.CityWeather = new Model({ mongodbConnector, collectionName: 'CityWeather' })
    models.Config = new Model({ mongodbConnector, collectionName: 'Config' })
    models.File = new Model({ mongodbConnector, collectionName: 'File' })
    models.SharePanel = new Model({ mongodbConnector, collectionName: 'SharePanel' })

    models.Article = new Model({ mongodbConnector, collectionName: 'Article' })

    // 51 Talent
    models.Resume = new Model({ mongodbConnector, collectionName: 'Resume', hardDelete: true })
    models.ResumeParse = new Model({ mongodbConnector, collectionName: 'ResumeParse', hardDelete: true })
    models.Enterprise = new Model({ mongodbConnector, collectionName: 'Enterprise' })

    // 系统信息
    models.SystemErrorLog = new Model({
      mongodbConnector,
      collectionName: 'SystemErrorLog',
      hardDelete: true,
      timestamps: {
        createdAt: false,
        updateAt: false
      }
    })
    models.SystemRequestLog = new Model({
      mongodbConnector,
      collectionName: 'SystemRequestLog',
      hardDelete: true,
      timestamps: {
        createdAt: false,
        updateAt: false
      }
    })

    // 维护索引
    const indexesConfig = config.INDEXES
    console.log('Db indexes checking...')
    const indexResult = await initIndexes(mongodbConnector.use('main'), indexesConfig, {
      dropNotExist: true
    })
    console.log('Db indexes maintenance finished :')
    console.log(JSON.stringify(indexResult))
  },
  models
}
